﻿// Decompiled with JetBrains decompiler
// Type: UnityEngine.Events.PersistentCall
// Assembly: UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E6AE6B5A-2752-4B07-9528-C8B77C62CE79
// Assembly location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.dll
// XML documentation location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.xml

using System;
using System.Reflection;
using UnityEngine.Serialization;

#nullable disable
namespace UnityEngine.Events
{
  [Serializable]
  internal class PersistentCall : ISerializationCallbackReceiver
  {
    [SerializeField]
    [FormerlySerializedAs("instance")]
    private UnityEngine.Object m_Target;
    [SerializeField]
    private string m_TargetAssemblyTypeName;
    [FormerlySerializedAs("methodName")]
    [SerializeField]
    private string m_MethodName;
    [SerializeField]
    [FormerlySerializedAs("mode")]
    private PersistentListenerMode m_Mode = PersistentListenerMode.EventDefined;
    [FormerlySerializedAs("arguments")]
    [SerializeField]
    private ArgumentCache m_Arguments = new ArgumentCache();
    [FormerlySerializedAs("enabled")]
    [FormerlySerializedAs("m_Enabled")]
    [SerializeField]
    private UnityEventCallState m_CallState = UnityEventCallState.RuntimeOnly;

    public UnityEngine.Object target => this.m_Target;

    public string targetAssemblyTypeName
    {
      get
      {
        if (string.IsNullOrEmpty(this.m_TargetAssemblyTypeName) && this.m_Target != (UnityEngine.Object) null)
          this.m_TargetAssemblyTypeName = UnityEventTools.TidyAssemblyTypeName(this.m_Target.GetType().AssemblyQualifiedName);
        return this.m_TargetAssemblyTypeName;
      }
    }

    public string methodName => this.m_MethodName;

    public PersistentListenerMode mode
    {
      get => this.m_Mode;
      set => this.m_Mode = value;
    }

    public ArgumentCache arguments => this.m_Arguments;

    public UnityEventCallState callState
    {
      get => this.m_CallState;
      set => this.m_CallState = value;
    }

    public bool IsValid()
    {
      return !string.IsNullOrEmpty(this.targetAssemblyTypeName) && !string.IsNullOrEmpty(this.methodName);
    }

    public BaseInvokableCall GetRuntimeCall(UnityEventBase theEvent)
    {
      if (this.m_CallState == UnityEventCallState.RuntimeOnly && !Application.isPlaying || this.m_CallState == UnityEventCallState.Off || theEvent == null)
        return (BaseInvokableCall) null;
      MethodInfo method = theEvent.FindMethod(this);
      if (method == null || !method.IsStatic && this.target == (UnityEngine.Object) null)
        return (BaseInvokableCall) null;
      UnityEngine.Object target = method.IsStatic ? (UnityEngine.Object) null : this.target;
      switch (this.m_Mode)
      {
        case PersistentListenerMode.EventDefined:
          return theEvent.GetDelegate((object) target, method);
        case PersistentListenerMode.Void:
          return (BaseInvokableCall) new InvokableCall((object) target, method);
        case PersistentListenerMode.Object:
          return PersistentCall.GetObjectCall(target, method, this.m_Arguments);
        case PersistentListenerMode.Int:
          return (BaseInvokableCall) new CachedInvokableCall<int>(target, method, this.m_Arguments.intArgument);
        case PersistentListenerMode.Float:
          return (BaseInvokableCall) new CachedInvokableCall<float>(target, method, this.m_Arguments.floatArgument);
        case PersistentListenerMode.String:
          return (BaseInvokableCall) new CachedInvokableCall<string>(target, method, this.m_Arguments.stringArgument);
        case PersistentListenerMode.Bool:
          return (BaseInvokableCall) new CachedInvokableCall<bool>(target, method, this.m_Arguments.boolArgument);
        default:
          return (BaseInvokableCall) null;
      }
    }

    private static BaseInvokableCall GetObjectCall(
      UnityEngine.Object target,
      MethodInfo method,
      ArgumentCache arguments)
    {
      System.Type type = typeof (UnityEngine.Object);
      if (!string.IsNullOrEmpty(arguments.unityObjectArgumentAssemblyTypeName))
        type = System.Type.GetType(arguments.unityObjectArgumentAssemblyTypeName, false) ?? typeof (UnityEngine.Object);
      ConstructorInfo constructor = typeof (CachedInvokableCall<>).MakeGenericType(type).GetConstructor(new System.Type[3]
      {
        typeof (UnityEngine.Object),
        typeof (MethodInfo),
        type
      });
      UnityEngine.Object @object = arguments.unityObjectArgument;
      if (@object != (UnityEngine.Object) null && !type.IsAssignableFrom(@object.GetType()))
        @object = (UnityEngine.Object) null;
      return constructor.Invoke(new object[3]
      {
        (object) target,
        (object) method,
        (object) @object
      }) as BaseInvokableCall;
    }

    public void RegisterPersistentListener(UnityEngine.Object ttarget, System.Type targetType, string mmethodName)
    {
      this.m_Target = ttarget;
      this.m_TargetAssemblyTypeName = UnityEventTools.TidyAssemblyTypeName(targetType.AssemblyQualifiedName);
      this.m_MethodName = mmethodName;
    }

    public void UnregisterPersistentListener()
    {
      this.m_MethodName = string.Empty;
      this.m_Target = (UnityEngine.Object) null;
      this.m_TargetAssemblyTypeName = string.Empty;
    }

    public void OnBeforeSerialize()
    {
      this.m_TargetAssemblyTypeName = UnityEventTools.TidyAssemblyTypeName(this.m_TargetAssemblyTypeName);
    }

    public void OnAfterDeserialize()
    {
      this.m_TargetAssemblyTypeName = UnityEventTools.TidyAssemblyTypeName(this.m_TargetAssemblyTypeName);
    }
  }
}
